﻿namespace OleDbDAL.Stock
{
    using DBUtility;
    using IDal.Stock;
    using Model.Stock;
    using Model.Systems;
    using System;
    using System.Data;
    using System.Text;
    using WinFormSession;

    public class LibraryDetailsCommApp : ILibraryDetailsComm
    {
        private string dbname = "Systems";

        public void Add(LibraryDetailsCommEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into T_LibraryDetailsComm(");
            builder.Append("DetailsId,ChainNum,LotNum,CommId,CommName,SupplierId,WarehouseId,DlId,InputPrice,Amount,AmoutUnit,ResidualNum,ResidualMoney,Remark");
            builder.Append(")");
            builder.Append(" values (");
            builder.Append("" + model.DetailsId + ",");
            builder.Append("" + model.ChainNum + ",");
            builder.Append("'" + model.LotNum + "',");
            builder.Append("" + model.CommId + ",");
            builder.Append("'" + model.CommName + "',");
            builder.Append("'" + model.SupplierId + "',");
            builder.Append("" + model.WarehouseId + ",");
            builder.Append("" + model.DlId + ",");
            builder.Append("" + model.InputPrice + ",");
            builder.Append("" + model.Amount + ",");
            builder.Append("" + model.AmoutUnit + ",");
            builder.Append("" + model.ResidualNum + ",");
            builder.Append("" + model.ResidualMoney + ",");
            builder.Append("'" + model.Remark + "'");
            builder.Append(")");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public void Delete(int OutInCommId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from T_LibraryDetailsComm ");
            builder.Append(" where OutInCommId=" + OutInCommId);
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }

        public DataTable GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_LibraryDetailsComm ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by OutInCommId ");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public int GetMaxId(string swhere)
        {
            return DbHelperOledb.GetMaxID(this.dbname, "OutInCommId", "T_LibraryDetailsComm", swhere);
        }

        public LibraryDetailsCommEnt GetModel(int OutInCommId)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select OutInCommId,DetailsId,ChainNum,LotNum,CommId,CommName,SupplierId,WarehouseId,DlId,InputPrice,Amount,AmoutUnit,ResidualNum,ResidualMoney,Remark from T_LibraryDetailsComm ");
            builder.Append(" where OutInCommId=" + OutInCommId);
            LibraryDetailsCommEnt ent = new LibraryDetailsCommEnt();
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            ent.OutInCommId = OutInCommId;
            if (table.Rows.Count > 0)
            {
                if (table.Rows[0]["DetailsId"].ToString() != "")
                {
                    ent.DetailsId = int.Parse(table.Rows[0]["DetailsId"].ToString());
                }
                if (table.Rows[0]["ChainNum"].ToString() != "")
                {
                    ent.ChainNum = int.Parse(table.Rows[0]["ChainNum"].ToString());
                }
                ent.LotNum = table.Rows[0]["LotNum"].ToString();
                if (table.Rows[0]["CommId"].ToString() != "")
                {
                    ent.CommId = int.Parse(table.Rows[0]["CommId"].ToString());
                }
                ent.CommName = table.Rows[0]["CommName"].ToString();
                ent.SupplierId = table.Rows[0]["SupplierId"].ToString();
                if (table.Rows[0]["WarehouseId"].ToString() != "")
                {
                    ent.WarehouseId = int.Parse(table.Rows[0]["WarehouseId"].ToString());
                }
                if (table.Rows[0]["DlId"].ToString() != "")
                {
                    ent.DlId = int.Parse(table.Rows[0]["DlId"].ToString());
                }
                if (table.Rows[0]["InputPrice"].ToString() != "")
                {
                    ent.InputPrice = decimal.Parse(table.Rows[0]["InputPrice"].ToString());
                }
                if (table.Rows[0]["Amount"].ToString() != "")
                {
                    ent.Amount = decimal.Parse(table.Rows[0]["Amount"].ToString());
                }
                if (table.Rows[0]["AmoutUnit"].ToString() != "")
                {
                    ent.AmoutUnit = int.Parse(table.Rows[0]["AmoutUnit"].ToString());
                }
                if (table.Rows[0]["ResidualNum"].ToString() != "")
                {
                    ent.ResidualNum = decimal.Parse(table.Rows[0]["ResidualNum"].ToString());
                }
                if (table.Rows[0]["ResidualMoney"].ToString() != "")
                {
                    ent.ResidualMoney = decimal.Parse(table.Rows[0]["ResidualMoney"].ToString());
                }
                ent.Remark = table.Rows[0]["Remark"].ToString();
                return ent;
            }
            return null;
        }

        public DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            string orderString = "";
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            builder.Append("select * from T_LibraryDetailsComm ");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), strWhere, orderString, out pageCount, out recordCount, "OutInCommId");
        }

        public int GetSearchDataCount(string swhere)
        {
            StringBuilder builder = new StringBuilder();
            UserEnt ent = (UserEnt) WinformSession.GetSession()["user"];
            builder.Append("select count(*) ");
            builder.Append(" from T_GoodsInOut,T_LibraryDetailsComm ");
            builder.Append(" where T_GoodsInOut.DetailsId=T_LibraryDetailsComm.DetailsId and T_GoodsInOut.ChainNum=" + ent.ChainNum);
            if (swhere != "")
            {
                builder.Append(swhere);
            }
            DataTable table = DbHelperOledb.Query(this.dbname, builder.ToString());
            if (table.Rows.Count > 0)
            {
                return int.Parse(table.Rows[0][0].ToString());
            }
            return 0;
        }

        public DataTable SearchData(string sWhere)
        {
            StringBuilder builder = new StringBuilder();
            UserEnt ent = (UserEnt) WinformSession.GetSession()["user"];
            builder.Append("select OutInCommId,");
            builder.Append(" DetailsType,ResidualNum,ResidualMoney,");
            builder.Append("(select parname from T_Parameter where T_GoodsInOut.LibraryTypeId=T_Parameter.ParId) as LibraryType,");
            builder.Append("CreateTime,LotNum,CommId,cOMMnAME,InputPrice,Amount,");
            builder.Append("(select parname from T_Parameter where T_LibraryDetailsComm.AmoutUnit=T_Parameter.ParId) as AmoutUnit");
            builder.Append(" from T_GoodsInOut,T_LibraryDetailsComm ");
            builder.Append(" where T_GoodsInOut.DetailsId=T_LibraryDetailsComm.DetailsId and T_GoodsInOut.ChainNum=" + ent.ChainNum);
            if (sWhere != "")
            {
                builder.Append(sWhere);
            }
            builder.Append(" order by CreateTime desc");
            return DbHelperOledb.Query(this.dbname, builder.ToString());
        }

        public DataTable SearchData(int PageSize, int PageIndex, string strWhere)
        {
            int pageCount = 0;
            int recordCount = 0;
            StringBuilder builder = new StringBuilder();
            UserEnt ent = (UserEnt) WinformSession.GetSession()["user"];
            builder.Append("select OutInCommId,");
            builder.Append("DetailsType,ResidualNum,ResidualMoney,");
            builder.Append("(select parname from T_Parameter where T_GoodsInOut.LibraryTypeId=T_Parameter.ParId) as LibraryType,");
            builder.Append("CreateTime,LotNum,CommId,cOMMnAME,InputPrice,Amount,");
            builder.Append("(select parname from T_Parameter where T_LibraryDetailsComm.AmoutUnit=T_Parameter.ParId) as AmoutUnit");
            builder.Append(" from T_GoodsInOut,T_LibraryDetailsComm ");
            builder.Append(" where T_GoodsInOut.DetailsId=T_LibraryDetailsComm.DetailsId and T_GoodsInOut.ChainNum=" + ent.ChainNum);
            if (strWhere != "")
            {
                builder.Append(strWhere);
            }
            builder.Append(" order by CreateTime desc");
            return DbHelperOledb.ExecutePager(this.dbname, PageIndex, PageSize, "", builder.ToString(), "", "$%", out pageCount, out recordCount, "OutInCommId");
        }

        public void Update(LibraryDetailsCommEnt model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update T_LibraryDetailsComm set ");
            builder.Append("DetailsId=" + model.DetailsId + ",");
            builder.Append("ChainNum=" + model.ChainNum + ",");
            builder.Append("LotNum='" + model.LotNum + "',");
            builder.Append("CommId=" + model.CommId + ",");
            builder.Append("CommName='" + model.CommName + "',");
            builder.Append("SupplierId='" + model.SupplierId + "',");
            builder.Append("WarehouseId=" + model.WarehouseId + ",");
            builder.Append("DlId=" + model.DlId + ",");
            builder.Append("InputPrice=" + model.InputPrice + ",");
            builder.Append("Amount=" + model.Amount + ",");
            builder.Append("AmoutUnit=" + model.AmoutUnit + ",");
            builder.Append("ResidualNum=" + model.ResidualNum + ",");
            builder.Append("ResidualMoney=" + model.ResidualMoney + ",");
            builder.Append("Remark='" + model.Remark + "'");
            builder.Append(" where OutInCommId=" + model.OutInCommId + "");
            DbHelperOledb.ExecuteSql(this.dbname, builder.ToString());
        }
    }
}

